
layui.define(['form', 'upload','selfUtils'], function(exports){
  var $ = layui.$
  ,selfUtils = layui.selfUtils
  ,layer = layui.layer
  ,laytpl = layui.laytpl
  ,setter = layui.setter
  ,rspCode = setter.response.statusCode
  ,view = layui.view
  ,admin = layui.admin
  ,form = layui.form
  ,upload = layui.upload;

  var $body = $('body');
  
  form.render();
  
  //自定义验证
  form.verify({
    nickname: function(value, item){ //value：表单的值、item：表单的DOM对象
      if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
        return '用户名不能有特殊字符';
      }
      if(/(^\_)|(\__)|(\_+$)/.test(value)){
        return '用户名首尾不能出现下划线\'_\'';
      }
      if(/^\d+\d+\d$/.test(value)){
        return '用户名不能全为数字';
      }
    }
    ,pass: [
      /^[\S]{6,12}$/
      ,'密码必须6到12位，且不能出现空格'
    ]
    
    //确认密码
    ,repass: function(value){
      if(value !== $('#new_password').val()){
        return '两次密码输入不一致';
      }
    }
  });

  //设置密码
  form.on('submit(setmypass)', function(obj){
    admin.req({
      url: '/admin/teacher/password/check'
      ,data: {"password":obj.field.oldPassword}
      ,done : function(res) {
        if(res.status == '200'){
          changePwd(layui.data(layui.setter.tableName)[layui.setter.USERNAME].username,obj.field.password);
        }else{
          selfUtils.alert.error(res.message)
        }
      },
      error:function(error) {
        if( error.status == rspCode.logout){
          layer.msg('登录状态已失效，跳转登录页面中', {
            icon: 9,
            time: 2000 //2秒关闭（如果不配置，默认是3秒）
          }, function(){
            admin.exit();
          });
        }
      }
    });
    return false;
  });

  function changePwd(username,password){
    //提交修改
    admin.req({
      url: '/admin/teacher/resetpassword'
      ,type:'PUT'
      ,data: {"password":password}
      ,done : function(res) {
        if(res.code == 200){
          selfUtils.alert.success("修改密码成功！即将返回重新登录");
          setTimeout(function () {
            admin.exit();
          },2000);
        }
      }
    });
  }
  
  //对外暴露的接口
  exports('set', {});
});